{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用了calendar.monthrange函数来获取每个月的天数，并通过一个forin的循环来累加这些天数，最后再加上给定日期的天数，从而得到结果。\n",
    "      \n",
    "（星期几，天数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2000年5月5日是这一年的第126天\n"
     ]
    }
   ],
   "source": [
    "import calendar\n",
    "year=int(input(\"请输入年份: \"))\n",
    "month=int(input(\"请输入月份: \"))\n",
    "day=int(input(\"请输入日期: \"))\n",
    "def day_of_year_using_calendar(year, month, day):\n",
    "    day_count = 0\n",
    "    for m in range(1, month):  \n",
    "        _, days_in_month = calendar.monthrange(year, m)\n",
    "        day_count += days_in_month\n",
    "    day_count += day \n",
    "    return day_count\n",
    "\n",
    "print(f\"{year}年{month}月{day}日是这一年的第{day_of_year_using_calendar(year, month, day)}天\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第三题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "is_prime 函数被用来判断一个给定的整数是否为素数。用2作为判断\n",
    "\n",
    "find_primes_up_to ，定义该程序，能够找出并返回小于或等于给定上限的所有素数列表。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的素数个数为: 168\n",
      "1000以内的所有素数为:\n",
      "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 "
     ]
    }
   ],
   "source": [
    "def is_prime(n, primes):\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    for prime in primes:\n",
    "        if prime * prime > n:\n",
    "            break  \n",
    "        if n % prime == 0:\n",
    "            return False  \n",
    "    return True  \n",
    "\n",
    "def find_primes_up_to(limit):\n",
    "    primes = [] \n",
    "    for num in range(2, limit + 1):\n",
    "        if is_prime(num, primes):\n",
    "            primes.append(num) \n",
    "    return primes\n",
    "limit = 1000\n",
    "\n",
    "primes = find_primes_up_to(limit)\n",
    "\n",
    "print(f\"1000以内的素数个数为: {len(primes)}\")\n",
    "\n",
    "print(\"1000以内的所有素数为:\")\n",
    "for prime in primes:\n",
    "    print(prime, end=' ')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第五题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过input函数获取用户输入的成绩，float并将其转换为浮点数。  \n",
    "然后，使用一个嵌套的三元运算符来判断成绩等级"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学生的成绩等级是： B\n"
     ]
    }
   ],
   "source": [
    "score = float(input(\"请输入学生的成绩：\"))\n",
    "grade = 'A' if score >= 90  else ('B' if score >= 60 else 'C')\n",
    "print(\"学生的成绩等级是：\", grade)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第七题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "count_characters 函数，这个函数将统计输入字符串中的英文字母、空格、数字和其他字符的数量。  \n",
    "isalpha() 是 Python 中的一个字符串方法，用于检查字符串中的字符是否都是字母。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数：10\n",
      "空格个数：3\n",
      "数字个数：5\n",
      "其他字符个数：0\n"
     ]
    }
   ],
   "source": [
    "def count_characters(s):\n",
    "    letters = sum(1 for c in s if c.isalpha())\n",
    "    spaces = sum(1 for c in s if c.isspace())\n",
    "    digits = sum(1 for c in s if c.isdigit())\n",
    "    others = sum(1 for c in s if not c.isalnum() and not c.isspace())\n",
    "    return letters, spaces, digits, others\n",
    "\n",
    "input_string = input(\"请输入一行字符串：\")\n",
    "letters, spaces, digits, others = count_characters(input_string)\n",
    "\n",
    "print(f\"英文字母个数：{letters}\")\n",
    "print(f\"空格个数：{spaces}\")\n",
    "print(f\"数字个数：{digits}\")\n",
    "print(f\"其他字符个数：{others}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第九题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义一个名为 find_perfect_numbers 的函数，接受一个整数 n 作为参数，表示要找出小于或等于 n 的所有完数。  \n",
    "初始化完数列表：创建一个空列表 perfect_numbers，用于存储找到的完数。  \n",
    "使用 for 循环遍历从1到 n 的所有整数。  \n",
    "对于每个数 num，初始化一个变量 factors_sum 来存储它的因子之和。  \n",
    "使用另一个 for 循环遍历从1到 num-1 的所有整数，检查它们是否是 num 的因子（通过 num % i == 0 判断）。  \n",
    "如果 i 是 num 的因子，则将 i 加到 factors_sum 中。  \n",
    "如果 factors_sum 等于 num，则将 num 添加到 perfect_numbers 列表中。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的完数有： [6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "def find_perfect_numbers(n):\n",
    "    perfect_numbers = []  \n",
    "    for num in range(1, n+1):\n",
    "        factors_sum = 0  \n",
    "        for i in range(1, num):\n",
    "            if num % i == 0:  \n",
    "                factors_sum += i  \n",
    "        if factors_sum == num:  \n",
    "            perfect_numbers.append(num)  \n",
    "    return perfect_numbers\n",
    "\n",
    "perfect_numbers = find_perfect_numbers(1000)\n",
    "print(\"1000以内的完数有：\", perfect_numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第十一题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义一个函数 sum_of_factorials(N) 来计算从1到 N 的所有阶乘之和。初始化 total_sum 为1（因为1的阶乘是1），然后使用 for 循环从2到 N 计算每个数的阶乘，并将其加到 total_sum 中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 + 2! + 3! + ... + 55! 的值是：12931604174610554792808543145134839786142796265765134511963408920420940313\n"
     ]
    }
   ],
   "source": [
    "def factorial(n):\n",
    "    if n == 0:\n",
    "        return 1\n",
    "    else:\n",
    "        return n * factorial(n-1)\n",
    "\n",
    "N = int(input(\"请输入一个正整数N：\"))\n",
    "def sum_of_factorials(N):\n",
    "    sum = 1  \n",
    "    for i in range(2, N+1):\n",
    "        sum += factorial(i)\n",
    "    return sum\n",
    "\n",
    "print(f\"1 + 2! + 3! + ... + {N}! 的值是：{sum_of_factorials(N)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第十三题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "检查输入的 N 是否为奇数，如果不是，则提示用户输入奇数并返回  \n",
    "使用 for 循环从 0 到 N // 2（包括 N // 2）。对于每一行，打印 (N // 2 - i) 个空格，然后打印 (2 * i + 1) 个星号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个奇数\n"
     ]
    }
   ],
   "source": [
    "N = int(input(\"请输入一个奇数N：\"))\n",
    "def print_diamond(N):\n",
    "    if N % 2 == 0:\n",
    "        print(\"请输入一个奇数\")\n",
    "        return\n",
    "\n",
    "    for i in range(N // 2 + 1):\n",
    "        print(' ' * (N // 2 - i) + '*' * (2 * i + 1))\n",
    "\n",
    "    for i in range(N // 2 - 1, -1, -1):\n",
    "        print(' ' * (N // 2 - i) + '*' * (2 * i + 1))\n",
    "\n",
    "print_diamond(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第十五题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 sorted() 函数对输入的数列进行排序。返回排序后的列表。  \n",
    "调用 get_numbers 函数获取用户输入的10个数。调用 sort_numbers 函数对数列进行排序。打印输入的数列和排序后的数列。    \n",
    "使用 if __name__ == \"__main__\": 确保当脚本被直接运行时执行 main 函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入10个数，每输入一个数后按回车键：\n",
      "输入的数列: [4534.0, 53.0, 453.0, 2.0, 34.0, 8534.0, 34.0, 535.0, 473454.0, 5.0]\n",
      "排序后的数列: [2.0, 5.0, 34.0, 34.0, 53.0, 453.0, 535.0, 4534.0, 8534.0, 473454.0]\n"
     ]
    }
   ],
   "source": [
    "def get_numbers():\n",
    "    numbers = []\n",
    "    print(\"请输入10个数，每输入一个数后按回车键：\")\n",
    "    for i in range(10):\n",
    "        while True:\n",
    "            try:\n",
    "                num = float(input(f\"输入第{i+1}个数：\"))\n",
    "                numbers.append(num)\n",
    "                break\n",
    "            except ValueError:\n",
    "                print(\"输入无效，请输入一个数字。\")\n",
    "    return numbers\n",
    "\n",
    "def sort_numbers(numbers):\n",
    "    return sorted(numbers)\n",
    "def main():\n",
    "    numbers = get_numbers()\n",
    "    sorted_numbers = sort_numbers(numbers)\n",
    "    print(\"输入的数列:\", numbers)\n",
    "    print(\"排序后的数列:\", sort_numbers(numbers))\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第十七题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这段代码首先定义了一个binary_search_insert函数，它使用二分查找法来找到目标数应该插入的位置。   \n",
    "如果arr[mid]等于目标值，那么找到了目标元素，返回mid。  \n",
    "如果arr[mid]小于目标值，说明目标值位于mid的右侧，更新low为mid + 1。  \n",
    "如果arr[mid]大于目标值，说明目标值位于mid的左侧，更新high为mid - 1   \n",
    "然后定义了insert_into_sorted_array函数，它调用binary_search_insert来找到插入位置，  \n",
    "并使用list.insert()方法将数插入到数组中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组： [1, 4, 8, 56, 545, 879, 999, 4266, 4541, 45645, 4562425]\n"
     ]
    }
   ],
   "source": [
    "def binary_search_insert(arr, target):\n",
    "    low, high = 0, len(arr) - 1\n",
    "    while low <= high:\n",
    "        mid = (low + high) // 2\n",
    "        if arr[mid] == target:\n",
    "            return mid \n",
    "        elif arr[mid] < target:\n",
    "            low = mid + 1\n",
    "        else:\n",
    "            high = mid - 1\n",
    "    return low  \n",
    "\n",
    "def insert_into_sorted_array(arr, target):\n",
    "    index = binary_search_insert(arr, target)\n",
    "    arr.insert(index, target)\n",
    "    return arr\n",
    "\n",
    "sorted_array = [1, 4, 8, 56, 545, 879, 999, 4266, 45645, 4562425]  \n",
    "number_to_insert = int(input(\"请输入一个数：\"))  \n",
    "new_array = insert_into_sorted_array(sorted_array, number_to_insert)\n",
    "print(\"插入后的数组：\", new_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第十九题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们首先创建一个列表 people，包含从1到n的编号。  \n",
    "然后使用 while 循环模拟报数的过程：  \n",
    "index 变量用来跟踪当前报数的人的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第8号的那位。\n"
     ]
    }
   ],
   "source": [
    "def last_person_standing(n):\n",
    "    people = list(range(1, n+1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)\n",
    "        people.pop(index)\n",
    "    return people[0]\n",
    "\n",
    "n = int(input(\"请输入一个数字n：\"))\n",
    "print(\"最后留下的是原来第{}号的那位。\".format(last_person_standing(n)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
